package br.com.robot.implement;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.http.client.ClientProtocolException;

import util.ER;
import util.Web;
import br.com.robot.RobotStatus;
import br.com.robot.interfaces.Robot;
import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.crawler.WebCrawler;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.url.WebURL;

public class Steam extends WebCrawler implements Robot
{
	private final static Pattern FILTERS = Pattern.compile( ".*(\\.(css|js|bmp|gif|jpe?g"   +
															"|png|tiff?|mid|mp2|mp3|mp4"    + 
															"|wav|avi|mov|mpeg|ram|m4v|pdf" +
															"|rm|smil|wmv|swf|wma|zip|rar|gz))$" );

	@Override
	public void init( int id, CrawlController crawlController )
	{
		super.init( id, crawlController );
	}
	
	/**
	 * You should implement this function to specify whether the given url
	 * should be crawled or not (based on your crawling logic).
	 */
	@Override
	public boolean shouldVisit( WebURL url ) 
	{
		String href = url.getURL( ).toLowerCase( );
		return !FILTERS.matcher( href ).matches( );
	}

	/**
	 * This function is called when a page is fetched and ready to be processed
	 * by your program.
	 */
	@Override
	public void visit( Page page )
	{
		if( page.getParseData( ) instanceof HtmlParseData ) 
		{
			List<WebURL> links = ((HtmlParseData)page.getParseData( )).getOutgoingUrls( );

			for( WebURL webURL : links )
			{
				if( webURL.getURL( ).matches( "http://store.steampowered.com/app/\\d*/\\?snr=.*$" ) )
				{
//					RobotController.gravaLista.addLine( webURL.getURL( ) );
				}
			}
		}
	}

	@Override
	public void iniciar( )
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void parar( )
	{
		// TODO Auto-generated method stub
	}

	@Override
	public RobotStatus verificarStatus( ) 
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public StringBuffer infLog( ) 
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> configuraSeeds( )
	{
		List<String> lst = new ArrayList<String>( );
		
		try 
		{
			String buscaExprecao = ER.buscaExprecao( Web.get( "http://store.steampowered.com/search/results?sort_order=ASC&page=1" ), 
					 "\\.\\.\\..*?<a href.*?>(.*?)</" );
			int totalPaginas = Integer.parseInt( buscaExprecao );
			
			for( int i = 1; i <= totalPaginas; i++ )
				lst.add( "http://store.steampowered.com/search/results?sort_order=ASC&page=" + i );
		} 
		catch( ClientProtocolException e )
		{
			e.printStackTrace( );
		} 
		catch( IOException e )
		{
			e.printStackTrace( );
		}

		return lst;
	}
}